package net.adst.workers;

import net.adst.resources.Constants;
import net.adst.ui.mainframe.MainFrame;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 01.11.12
 * Time: 4:57
 * To change this template use File | Settings | File Templates.
 */
public class MainFramePaintWorker extends SwingWorker<Void, Void> {
    private MainFrame frame;

    public MainFramePaintWorker(MainFrame frame) {
        this.frame = frame;
    }

    @Override
    protected Void doInBackground() throws Exception {
        frame.init();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (confirmExit(e)) {
                    exit();
                }
            }
        });
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setMinimumSize(new Dimension(Constants.MIN_WIDTH, Constants.MIN_HEIGHT));
        frame.setSize(new Dimension(Constants.DEF_WIDTH, Constants.DEF_HEIGHT));
        return null;
    }

    private boolean confirmExit(WindowEvent e) {
//        if (Boolean.valueOf(ResourceManager.getSettingString("auto.exit"))) {
//            return true;
//        }
        int result = JOptionPane.showConfirmDialog(e.getWindow(),
                "Are you sure you want to exit the application?",
                "Exit Application", JOptionPane.YES_NO_OPTION);
        return result == JOptionPane.YES_OPTION;
    }

    private void exit() {
//        ((UpdateThread) DataManager.getObject(ResourceManager.getVariableName("thread.UpdateThread"))).interrupt();
        System.exit(0);
    }
}
